package guitforl.wrappers

import guitforl.parsers.xml.XMLWidgetNodeParser._
import xml.NodeSeq
import guitforl.data.readers.FileReader
import guitforl.wrappers.ChartTypes._

/**
 * Created by IntelliJ IDEA.
 * User: artem
 * Date: Jul 13, 2010
 * Time: 11:38:38 PM
 * To change this template use File | Settings | File Templates.
 */

object WrappersFactory {
  
	def createChartFromXML(chartXMLRepresentation: NodeSeq) = {
		val name = getWidgetName(chartXMLRepresentation)
		val caption = getWidetCaption(chartXMLRepresentation)
    val xCaption = getXAxeCaption(chartXMLRepresentation)
    val yCaption = getYAxeCaption(chartXMLRepresentation)
		val chartType = ChartTypes(getChartType(chartXMLRepresentation))
		val dataSource = createDataSources(chartXMLRepresentation)
		new ChartWrapper(name, caption, xCaption, yCaption, chartType, dataSource)
	}

	def createEditFromXML(editXMLRepresentation: NodeSeq) = {
    val caption = getWidetCaption(editXMLRepresentation)
		val paramName = getEditParamName(editXMLRepresentation)
		val defaultValue = getDefaultValue(editXMLRepresentation)
		new EditWrapper(caption, paramName, defaultValue)
	}

	def createLabelFromXML(labelXMLRepresentation: NodeSeq) = {
		val name = getWidgetName(labelXMLRepresentation)
	  val defaultValue = getDefaultValue(labelXMLRepresentation)
		val dataSource = createDataSources(labelXMLRepresentation)
		new LabelWrapper(name, defaultValue, dataSource)
	}

	def createTableFromXML(tableXMLRepresentation: NodeSeq) = {
		val name = getWidgetName(tableXMLRepresentation)
		val caption = getWidetCaption(tableXMLRepresentation)
		val dataSource = createDataSources(tableXMLRepresentation)
		new TableWrapper(name, caption, dataSource)
	}

	private def createDataSources(widgetNode: NodeSeq) = {
		val dataFilesNames = getDataSourceFileNames(widgetNode)
		dataFilesNames.map( new FileReader(_) )
	}

}